//
//  DHMainController.swift
//  Weibo
//
//  Created by Holfioo on 15/8/31.
//  Copyright © 2015年 Holfioo. All rights reserved.
//

import UIKit

class DHMainController: UITabBarController {

    override func viewDidLoad() {
        super.viewDidLoad()
        // setupTabbarView()
        // 设施tb的颜色
        addChildViewControllers()
    }
    
    override func loadView() {
        super.loadView()
        // tabbar是空的
        // print(tabBar.subviews)
    }
    override func viewWillAppear(animated: Bool) {
        super.viewWillAppear(animated)
        // 按钮增加到tabbar中心
        setBtnFrame()
    }
    override func viewDidAppear(animated: Bool) {
        // print(tabBar.subviews.count)
        // 子控件数量是8个
        super.viewDidAppear(animated)
    }

    // 消息循环机制。由于btn方法是私有的。由于swift在方法执行的时候比较严格。
    // @objc 特殊声明一下此方法是oc的方法。
    @objc private func addBtnClick() {
        // printLog(__FUNCTION__)
        // 判断用户的登录状态
        let vc : UIViewController = DHUserAccountViewModel.sharedUserAccount.userLogin ? ComposeViewController() : OAuth()
        let nav = UINavigationController(rootViewController: vc)
        presentViewController(nav, animated: true, completion: nil)
    }
    
    private func setBtnFrame(){
        //  let btn = addBtnView()
        // tabar.subviews.count
        let count = childViewControllers.count
        // 计算宽度
        let w = tabBar.bounds.width / CGFloat(count)
        // let rect = CGRect(x: 0, y: 0, width: w, height: tabBar.bounds.height)
        // 此方法是对rect进行移动
        //  addBtnView.frame = CGRectOffset(rect, w * 2, 0)
        addBtnView.frame = CGRect(x: w * 2, y: 0, width: w, height: tabBar.bounds.height)
    }
    // 懒加载按钮
    private lazy var addBtnView: UIButton = {
        let btn = UIButton(type: UIButtonType.Custom)
        btn.setImage(UIImage(named: "tabbar_compose_icon_add"), forState: UIControlState.Normal)
        btn.setImage(UIImage(named: "tabbar_compose_icon_add_highlighted"), forState: UIControlState.Highlighted)
        btn.setBackgroundImage(UIImage(named: "tabbar_compose_button"), forState: UIControlState.Normal)
        btn.setBackgroundImage(UIImage(named: "tabbar_compose_button_highlighted"), forState: UIControlState.Highlighted)
        // 设置圆角
        btn.layer.cornerRadius = 30
        btn.layer.masksToBounds = true
        self.tabBar.addSubview(btn)
        // 增加点击事件
        btn.addTarget(self, action: "addBtnClick", forControlEvents: UIControlEvents.TouchUpInside)
        return btn
    }()
    
    
    // 增加子试图
    private func addChildViewControllers() {
        setupTBView(DHHomeController(), title: "首页", image: "tabbar_home")
        setupTBView(DHMessageController(), title: "消息", image: "tabbar_message_center")
        // 增加中间的+号
        addChildViewController(UIViewController())
        setupTBView(DHDiscoverController(), title: "发现", image: "tabbar_discover")
        setupTBView(DHMeController(), title: "我", image: "tabbar_profile")
    }

    private func setupTBView(vc: UIViewController, title: String, image: String) {
        
        // 创建tableView
        // let tb = vc
        /**
        *  负责控制tabbar的 title
        *   以及nav的titile
        */
        // vc.title = "首页"
        vc.title = title
        vc.tabBarItem.image = UIImage(named: image)
        
        // 导航控制器
        let nv = UINavigationController(rootViewController: vc)
        addChildViewController(nv)
    }

}
